package cn.itcast.web.controller.cargo;

import cn.itcast.domain.cargo.Export;
import cn.itcast.domain.cargo.ExportExample;
import cn.itcast.domain.cargo.Packing;
import cn.itcast.domain.cargo.PackingExample;
import cn.itcast.service.cargo.ExportService;
import cn.itcast.service.cargo.PackingService;
import cn.itcast.web.controller.BaseController;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/cargo/packing")
public class PackingController extends BaseController {

    @Reference(retries = 0)
    private PackingService packingService;
    @Reference(retries = 0)
    private ExportService exportService;

    /**
     *  生成装箱单之前后端校验状态，排除当前用户之外的其他用户修改数据，保证数据准确性。
     * @return
     */
    @RequestMapping("/checkState")
    @ResponseBody
    public Boolean checkState(String[] exportIds) {
        ExportExample exportExample = new ExportExample();
        exportExample.createCriteria().andIdIn(Arrays.asList(exportIds));
        List<Export> exportList = exportService.findAll(exportExample);
        for (Export export : exportList) {
            if (export.getState() != 2) {       //(后期数据字典优化)
                return false;
            }
        }
        return true;
    }

    /**
     *  生成装箱单
     *  多个装箱单要求装运港、目的港、收货人必须一致
     * @param exportIds 报运单id
     * @return
     */
    @RequestMapping("/createPacking")
    public String createPacking(String[] exportIds) {
        //单个报运单，直接生成装箱单
        if (exportIds.length == 1) {
            Packing packing = packingService.createPacking(exportIds);
            request.setAttribute("packing", packing);
            return "cargo/export/export-addPacking";
        }
        //根据exportIds获取exportList
        boolean flag = true;
        ExportExample exportExample = new ExportExample();
        exportExample.createCriteria().andIdIn(Arrays.asList(exportIds));
        List<Export> exportList = exportService.findAll(exportExample);
        //获取第一个export
        Export export = exportList.get(0);
        String shipmentPort = export.getShipmentPort();
        String destinationPort = export.getDestinationPort();
        String consignee = export.getConsignee();
        //判断数据有效性
        if (shipmentPort != null && destinationPort != null && consignee != null) {
            //比较装运港、目的港、收货人信息是否相同
            for (int i = 1; i < exportList.size(); i++) {
                Export exportItem = exportList.get(i);
                if (exportItem.getShipmentPort() == null ||
                        !exportItem.getShipmentPort().equals(shipmentPort)) {
                    flag = false;
                }
                if (exportItem.getDestinationPort() == null ||
                        !exportItem.getDestinationPort().equals(destinationPort)) {
                    flag = false;
                }
                if (exportItem.getConsignee() == null ||
                        !exportItem.getConsignee().equals(consignee)) {
                    flag = false;
                }
            }
        } else {
            flag = false;
        }
        //判断装运港、目的港、收货人数据是否一致
        if (flag) {
            //数据一致，生成装装箱单
            Packing packing = packingService.createPacking(exportIds);
            request.setAttribute("packing", packing);
            return "cargo/export/export-addPacking";
        } else {
            //如果数据不一致，则进入错误页面
            request.setAttribute("error", "生成装箱单失败");
            request.setAttribute("errorMsg", " 报运单的装运港、目的港、收货人必须一致");
            return "/mistake";
        }
    }

    /**
     *  添加装箱单
     * @param packing 装箱单
     * @return
     */
    @RequestMapping("/addPacking")
    public String addPacking(Packing packing) {
        packing.setCreateBy(getLoginUser().getId());
        packing.setCreateDept(getLoginUser().getDeptId());
        packing.setCreateTime(new Date());
        packing.setCompanyId(getLoginCompanyId());
        packing.setCompanyName(getLoginCompanyName());
        //设置装箱单状态为"草稿"  (后期数据字典优化)
        packing.setState(0);
        packingService.addPacking(packing);
        return "redirect:/cargo/export/list";
    }

    @RequestMapping("/list")
    public String list(@RequestParam(defaultValue = "1") int pageNum,
                       @RequestParam(defaultValue = "10") int pageSize) {
        PackingExample packingExample = new PackingExample();
        packingExample.createCriteria().andCompanyIdEqualTo(getLoginCompanyId());
        PageInfo<Packing> pageInfo = packingService.findByPage(packingExample, pageNum, pageSize);
        request.setAttribute("pageInfo", pageInfo);
        return "cargo/packing/packing-list";
    }

    @RequestMapping("/toView")
    public String toView(String packingListId) {
        Packing packing = packingService.findById(packingListId);
        request.setAttribute("packing", packing);
        return "cargo/packing/packing-view";
    }

    @RequestMapping("/delete")
    public String delete(String packingListId) {
        packingService.delete(packingListId);
        return "redirect:/cargo/packing/list";
    }
}
